﻿@using Seal.Model;
@{
    SecurityProvider provider = Model;
    
    provider.Script = @"@using System.Diagnostics
@using System.Security.Claims
@{
    SecurityUser user = Model;

    if (user.Request != null) {    
        //Get ClaimsPrincipal from Http request (e.g. For Azure App Service)
        var principal = AzureHelper.ClaimsPrincipalParse(user.Request);

        /* List of information available
            var str = """";
            foreach (Claim claim in principal.Claims)
            {
              str += $""CLAIM TYPE:'{claim.Type}'; CLAIM VALUE:'{claim.Value}'\r\n"";
            }
            Helper.WriteLogEntry(""Audit"", EventLogEntryType.Warning, str);
        */
        
        //Set user name
        user.WebUserName = principal.Claims.First(i => i.Type == ""name"").Value;
        user.Name = principal.Claims.First(i => i.Type == ""name"").Value;

        //Set groups
        foreach (var gr in user.Security.Groups) 
        {
            if (principal.IsInRole(gr.Name)) {
                user.AddSecurityGroup(gr.Name);
            }
        }
        
        if (user.SecurityGroups.Count == 0)		
        {
            user.Error = ""The user is authenticated but he does not belong to any security group."";
            Helper.WriteLogEntry(""Web Report Server"", EventLogEntryType.Warning, user.Error);            
        }    
    } 
    else 
    {
        user.Error = ""The user is not authenticated."";
    }
}";
}

